package com.huike.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.dto.ClueRecordDto;
import com.huike.mapper.ClueRecordMapper;
import com.huike.pojo.BusinessOpportunity;
import com.huike.pojo.ClueFollow;
import com.huike.result.PageResult;
import com.huike.service.ClueRecordService;
import com.huike.service.ClueService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ClueRecordServiceImpl extends ServiceImpl<ClueRecordMapper, ClueFollow> implements ClueRecordService {
    private final ClueRecordMapper clueRecordMapper;
    private final ClueService clueService;
    @Override
    public PageResult getClueRecordList(Integer clueId) {
        List<ClueFollow> followList = clueRecordMapper.selectList(Wrappers.<ClueFollow>lambdaQuery().eq(ClueFollow::getClueId, clueId));
        return new PageResult(followList.size(),followList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addClueRecord(ClueRecordDto clueRecordDto) {
        //需要新增跟进记录，更新线索表数据
        ClueFollow clueFollow = new ClueFollow();
        BeanUtil.copyProperties(clueRecordDto, clueFollow);
        clueFollow.setId(null);
        clueRecordMapper.insert(clueFollow);
        LambdaUpdateWrapper<BusinessOpportunity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BusinessOpportunity::getId, clueFollow.getClueId())
                .set(BusinessOpportunity::getAge,clueRecordDto.getAge())
                .set(BusinessOpportunity::getName,clueRecordDto.getName())
                .set(BusinessOpportunity::getWeixin,clueRecordDto.getWeixin())
                .set(BusinessOpportunity::getQq,clueRecordDto.getQq())
                .set(BusinessOpportunity::getSubject,clueRecordDto.getSubject())
                .set(BusinessOpportunity::getNextTime,clueRecordDto.getNextTime())
                .set(BusinessOpportunity::getStatus,ClueServiceImpl.CLUE_FOLLOW_STATUS)
                .set(BusinessOpportunity::getSex,clueRecordDto.getSex());
        clueService.update(updateWrapper);
    }
}
